import 'package:dio/dio.dart';
import 'package:hong_vpn_new_20240626/hvn_tools/vpn_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'configs/dio_log_interceptor.dart';
import 'models/vpn_list.dart';

class Global {
  static String serviceUrl = "https://hotnetwork.site/service.html";
  static String privacyUrl = "https://hotnetwork.site/privacy.html";

  static List<VpnListModel> vpns = []; // VPN 列表

  static final ovpnStatus = VPNStatus();

  static const String isReadPrivacyCacheKey = "isReadPrivacyCacheKey";
  static bool get isReadPrivacy => _isReadPrivacy;
  static bool _isReadPrivacy = false;
  static set isReadPrivacy(bool isReadPrivacy) {
    _isReadPrivacy = isReadPrivacy;
    _isReadPrivacyCache();
  }

  static _isReadPrivacyCache() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setBool(isReadPrivacyCacheKey, isReadPrivacy);
  }

  static reloadIsReadPrivacy() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    isReadPrivacy = prefs.getBool(isReadPrivacyCacheKey) ?? false;
  }

  static VpnListModel? get currVpn => VpnManager.share.currVpn; // 当前连接的VPN
  static set currVpn(VpnListModel? currVpn) {
    VpnManager.share.currVpn = currVpn;
  }

  static Dio get dio => _initDio();

  static Dio? _dio;
  static Dio _initDio() {
    if (_dio == null) {
      BaseOptions baseOptions = BaseOptions(
        //基本网址
        // baseUrl: "https://aidreamworld.fun/",
        // baseUrl: "https://maxmusic.fun/",
        //连接超时
        connectTimeout: const Duration(milliseconds: 30000),
        //接收超时
        receiveTimeout: const Duration(milliseconds: 500000),
        // //包头
        // headers: {
        //   "Content-Type": "application/json;Charset=UTF-8",
        //   "connect": "get"
        // },
        //内容类型
        contentType: 'application/json;Charset=UTF-8',
        //响应类型——期待已那种方式接收数据，默认是json
        responseType: ResponseType.json,
      );
      _dio = Dio(baseOptions);
      _dio?.interceptors.add(DioLogInterceptor());
    }
    return _dio!;
  }
}

enum UserRole { assistant, user, sys, error, loading, welcome }

UserRole kUserRoleRawValue(String rawValue) => switch (rawValue) {
      "assistant" => UserRole.assistant,
      "user" => UserRole.user,
      "system" => UserRole.sys,
      "error" => UserRole.error,
      "loading" => UserRole.loading,
      "welcom" => UserRole.welcome,
      _ => UserRole.user
    };

extension UserRoleValue on UserRole {
  String get rawValue => switch (this) {
        UserRole.assistant => "assistant",
        UserRole.user => "user",
        UserRole.sys => "system",
        UserRole.error => "error",
        UserRole.loading => "loading",
        UserRole.welcome => "welcom"
      };
}
